package com.sdy.dao.redis;

import com.alibaba.fastjson.JSONObject;
import com.sdy.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.UUID;

@Repository
@SuppressWarnings("all")
public class CompanyDao {

    @Autowired
    RedisUtil redisUtil;

    /**
     * 获取客户识别码，如果没有就set
     * @param companyId
     * @return
     */
    public String getConpanyCode(Integer companyId){
        System.out.println("111111111111");
        if (isCodeExist(companyId)){
            System.out.println("22222222222");
            return (String) redisUtil.get("companyCode:" + companyId );
        }
        return null;
    }

    public Boolean isCodeExist(Integer companyId){
        return redisUtil.hasKey("companyCode:" + companyId);
    }

    public Boolean setConpanyCode(Integer companyId,String code,Integer time){
        try {
            redisUtil.set("companyCode:" + companyId ,code,time);
            return true;
        }catch (Exception e){
            System.out.println(e);
            return false;
        }
    }

    //用于创建websocket连接的键值对
    public Boolean setConnectCode(Integer companyId,String code,Integer time){
        try {
            System.out.println(code + "    " + companyId);
            redisUtil.set("companyCode:" + code ,companyId,time);
            return true;
        }catch (Exception e){
            System.out.println(e);
            return false;
        }
    }
}
